<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/10/28
 * Time: 10:14
 */

namespace Wx;

use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Common\Model\WeixinModel;
use Think\Controller;
class BaseController extends Controller {

    protected $smarty=null;
    protected $isWechat=0;
    public function _empty(){
        $this->callMethod();
    }



    private function wxLogin(){
        //$openid     = isset($_REQUEST['openid']) ? $_REQUEST['openid'] : null;
        $gourl   = session('redirect');
        header("Content-Type: text/html;charset=utf-8");
        $string = urldecode($_SERVER['QUERY_STRING']);
        $ret = array();
        @parse_str($string, $ret);

        //若获取不到openid
        if (!$ret['openid']) {
            $url = WECHAT_AUTHORIZATION_URI."backurl=" . urlencode($gourl);
            header("Location:" . $url);
            die;
        }

        //若获取不到nickname
        if (!$ret['nickname']) {
            $url = WECHAT_AUTHORIZATION_GET_USERINFO_URI."backurl=" . urlencode($gourl);
            header("Location:" . $url);
            die;
        }


        $res = UserModel::wechatLogin($ret['openid'], $ret['nickname'], $ret['headimgurl'],$ret['sex'],session('WCapp') );
        session('uid',$res['uid']);
        
        //复制获取openid
        session('openid',$ret['openid']);
        //session('unionid',$ret['unionid']);

        $redirect = session('redirect');
        session('redirect',null);
        session('starLogin',1);
        header("Location: ".$redirect);
        die;
    }




    /**
     *
     * Author: shijy
     */
    function _initialize(){
        UtilsModel::checkMaintenance();

        //确定当下是哪个微信公众号，每个应用需要标注自自哪个网辰微信
        if(array_key_exists('WCapp',$_GET)){
            session('WCapp',$_GET['WCapp']);
        }

        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $this->isWechat = 0;
        if(strpos($user_agent, 'MicroMessenger') !== false ) {
            UtilsModel::disableClientAndTip(ENABLE_WEIXIN);


            $this->isWechat = 1;
            if( !session('starLogin')) {
                session('redirect',$_SERVER['REQUEST_URI']);
                 $this->wxLogin();
                 return;
            }
        }
        else{
            UtilsModel::disableClientAndTip(ENABLE_WAP);
        }


        $this->smarty=getSmarty();

        //全局传用户信息
        $this->smarty->assign('userinfo',array());
        if(session('uid')){
            session('tip',S('tip_'.session('uid')));
            $this->smarty->assign('userinfo',session());
        }
        if(!IS_AJAX){
            S('tip_'.session('uid'),null);
        }

        //微信分享
        $this->smarty->assign('isWechat',$this->isWechat);
        if($this->isWechat){
            $wxReturn = WeixinModel::getSignPackage();
            if($wxReturn['status']==0){
                $this->smarty->assign('wxSign',$wxReturn['data']);
            }
        }

        if(!session('lastJoinTime')) session('lastJoinTime', time()) ;




    }



    /**
     * 重置session的参数
     * User: Rick.Sun.<sunwgjj@126.com>
     * Return: void
     * Address: 浙江宣逸网络科技有限公司
     * @param $data
     */
    function resetUserSessions($data){
        foreach ($data as $k => $v) {
            session($k, $v);
        }
    }

    public function callMethod(){
        //检查公司专属函数，检查平台专属函数
//        $method=ACTION_NAME.'_'.CURRENT_COMPANY;
//        if(!method_exists($this,$method )){
//            $method=ACTION_NAME.'_'.OTHER_PART;
//            if(!method_exists($this,$method )) {
//                $this->error('未定义的方法:' . $method);
//            }
//        }
//
//        $ref_class = new \ReflectionClass($this);
//        $instance  = $ref_class->newInstance();
//        $method = $ref_class->getmethod($method);
//        $method->setAccessible(true);
//        $method->invoke($instance);
    }


}